import config from "@/config/index";
import {
	useUserStore
} from "@/store/modules/user"
import {
	useSystemStore
} from "@/store/modules/system"

const systemConfig = useSystemStore()
const userConfig = useUserStore()

function post(path = "", params = {}, mouseApi = false, optional = {
	responseType: 0,
	contentType: 'application/x-www-form-urlencoded',
	timeout: 120000,
}) {
	let global = null;
	if (getApp()) {
		global = getApp().globalData;
		params.channel = global.channel
	}
	let url = config.BASE_URL + path;
	params.identifier = Number.parseInt(new Date().getTime() / 1000);

	//客户端参数
	params.version = config.VERSION;
	params.channelId = config.CHANNEL_ID;
	params.plat = config.PLATFORM;

	//客户端缓存参数
	params.openId = userConfig.openId;
	params.token = userConfig.token;
	params.system = global?global.system:uni.getSystemInfoSync().osName;

	// let shareInfo = getShareInfo();
	// if (shareInfo) {
	// 	params.shareUserId = shareInfo.uid;
	// 	params.shareType = shareInfo.type;
	// }
	params.checksum = sign(params);
	console.log('http->request:', url, params);
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			data: params,
			header: {
				'Content-Type': optional.contentType || 'application/x-www-form-urlencoded'
			},
			method: 'POST',
			timeout: optional.timeout || 120000,
			success(rep) {
				let res = rep.data;
				console.log('http->response:', res);
				if ((optional.responseType || 0) === 0) {
					if (res['code'] === 200) {
						resolve(res['data']);
					} else {
						if (res['code'] === 201) {
							//重新登录
							// clearToken()
							// clearOpenId()
							// removeUserCache()
							// login();
						}
						reject(res);
					}
				} else {
					resolve(res);
				}
			},
			fail(err) {
				console.warn('http error:', err);
				reject(err);
			}
		});
	})
}
export default post;